package com.gscitysfy.cus.drain.modules.command.convert;

import com.gscitysfy.cloudfrmwk.assistant.domain.Result;
import com.gscitysfy.cloudfrmwk.portal.feign.DistrictService;
import com.gscitysfy.cus.drain.modules.command.dto.DangerSituationManageDto;
import com.gscitysfy.cus.drain.modules.command.entity.DangerSituationManage;
import com.gscitysfy.cus.drain.modules.common.entity.DrainInspectionMnFile;
import com.gscitysfy.cus.drain.modules.common.mapper.DrainInspectionMnFileMapper;
import lombok.AllArgsConstructor;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @Author jiang_wen
 * @Date 2022/10/20 21:11
 * @Description TODO
 */
@AllArgsConstructor
@Component
public class DangerSituationManageConvert {

    private final DrainInspectionMnFileMapper fileMapper;
    private final DistrictService districtService;

    public DangerSituationManage toEntity(DangerSituationManage dangerSituationManage, DangerSituationManageDto dangerSituationManageDto) {
        SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        dangerSituationManage.setName(dangerSituationManageDto.getName());
        dangerSituationManage.setAddress(dangerSituationManageDto.getAddress());
        dangerSituationManage.setPondingDepth(dangerSituationManageDto.getPondingDepth());
        dangerSituationManage.setSource(dangerSituationManageDto.getSource());
        dangerSituationManage.setAlarmId(dangerSituationManageDto.getAlarmId());

        dangerSituationManage.setPublishStatus(dangerSituationManageDto.getPublishStatus());
        try {
            if (StringUtils.isNotEmpty(dangerSituationManageDto.getPublishTime())) {
                dangerSituationManage.setPublishTime(sdf1.parse(dangerSituationManageDto.getPublishTime()));
            }
        }catch (Exception e){
            dangerSituationManage.setPublishTime(null);
        }
        dangerSituationManage.setCmCode(dangerSituationManageDto.getCmCode());
        dangerSituationManage.setDispatchFlag(dangerSituationManageDto.getDispatchFlag());
        dangerSituationManage.setDiscoveryTime(StringUtils.isNotEmpty(dangerSituationManageDto.getDiscoveryTime()) ? LocalDateTime.parse(dangerSituationManageDto.getDiscoveryTime(), DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")) : null);
        dangerSituationManage.setLongitude(dangerSituationManageDto.getLongitude());
        dangerSituationManage.setLatitude(dangerSituationManageDto.getLatitude());
        dangerSituationManage.setDescribe(dangerSituationManageDto.getDescribe());
        dangerSituationManage.setFileId(dangerSituationManageDto.getFileId());
        dangerSituationManage.setCategory(dangerSituationManageDto.getCategory());
        dangerSituationManage.setCloseFlag(dangerSituationManageDto.getCloseFlag());
        dangerSituationManage.setCmRegflag(StringUtils.isNotEmpty(dangerSituationManageDto.getCmRegflag()) ? Long.parseLong(dangerSituationManageDto.getCmRegflag()) : null);
        return dangerSituationManage;
    }


    public DangerSituationManageDto toDto(DangerSituationManage dangerSituationManage) {
        SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        DangerSituationManageDto dangerSituationManageDto = new DangerSituationManageDto();

        dangerSituationManageDto.setCmCode(dangerSituationManage.getCmCode());

        dangerSituationManageDto.setAlarmId(dangerSituationManage.getAlarmId());
        if(dangerSituationManage.getPublishTime()!=null){
            dangerSituationManageDto.setPublishTime(sdf1.format(dangerSituationManage.getPublishTime()));
        }
        dangerSituationManageDto.setPublishStatus(dangerSituationManage.getPublishStatus());
//        dangerSituationManageDto.setPublishTime(Objects.nonNull(dangerSituationManage.getPublishTime()) ? DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss").withZone(ZoneId.systemDefault()).format(dangerSituationManage.getPublishTime()) : null);


        dangerSituationManageDto.setCmKid(dangerSituationManage.getCmKid());
        dangerSituationManageDto.setName(dangerSituationManage.getName());
        dangerSituationManageDto.setDispatchContent(dangerSituationManage.getDispatchContent());
        dangerSituationManageDto.setAddress(dangerSituationManage.getAddress());
        dangerSituationManageDto.setPondingDepth(dangerSituationManage.getPondingDepth());
        dangerSituationManageDto.setSource(dangerSituationManage.getSource());
        dangerSituationManageDto.setDispatchFlag(dangerSituationManage.getDispatchFlag());
        dangerSituationManageDto.setDiscoveryTime(Objects.nonNull(dangerSituationManage.getDiscoveryTime()) ? DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss").withZone(ZoneId.systemDefault()).format(dangerSituationManage.getDiscoveryTime()) : null);
        dangerSituationManageDto.setLongitude(dangerSituationManage.getLongitude());
        dangerSituationManageDto.setLatitude(dangerSituationManage.getLatitude());
        dangerSituationManageDto.setDescribe(dangerSituationManage.getDescribe());
        dangerSituationManageDto.setFileId(dangerSituationManage.getFileId());
        if (Objects.nonNull(dangerSituationManage.getFileId())) {
            DrainInspectionMnFile drainInspectionMnFile = fileMapper.selectById(dangerSituationManage.getFileId());
            if (Objects.nonNull(drainInspectionMnFile)) {
                dangerSituationManageDto.setFileName(drainInspectionMnFile.getCmFileName());
            }
        }
        dangerSituationManageDto.setCategory(dangerSituationManage.getCategory());
        dangerSituationManageDto.setCloseFlag(dangerSituationManage.getCloseFlag());
        dangerSituationManageDto.setCmRegflag(String.valueOf(dangerSituationManage.getCmRegflag()));
        if (Objects.nonNull(dangerSituationManage.getCmRegflag())) {
            Result<String> result = districtService.getDistrictNameByCode(String.valueOf(dangerSituationManage.getCmRegflag()));
            if (result.getStatus() == 200) {
                dangerSituationManageDto.setCmRegflagName(result.getData());
            }
        }
        dangerSituationManageDto.setReservePlanId(dangerSituationManage.getReservePlanId());
        dangerSituationManageDto.setReservePlanTime(Objects.nonNull(dangerSituationManage.getReservePlanTime()) ? DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss").withZone(ZoneId.systemDefault()).format(dangerSituationManage.getReservePlanTime()) : null);
        dangerSituationManageDto.setResourceDispatchTime(Objects.nonNull(dangerSituationManage.getResourceDispatchTime()) ? DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss").withZone(ZoneId.systemDefault()).format(dangerSituationManage.getResourceDispatchTime()) : null);
        dangerSituationManageDto.setResourceDispatchSuggest(dangerSituationManage.getResourceDispatchSuggest());
        return dangerSituationManageDto;
    }

    public List<DangerSituationManageDto> toDtoList(List<DangerSituationManage> dangerSituationManageList) {
        if (CollectionUtils.isEmpty(dangerSituationManageList)) {
            return Collections.emptyList();
        }
        return dangerSituationManageList.stream().map(this::toDto).collect(Collectors.toList());
    }
}
